

export default {
  hostUrl:"http://127.0.0.1:9000",
  sendAjax(url:string, data:object, method:string, callback:Function) {
    console.log("----开始请求----");
    let xhr = new XMLHttpRequest();
    xhr.open(method,this.hostUrl+url);
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

    let params = this.serialize(data)

    //{"username":"","password":""}
    //username=xxx&password=xxx
    xhr.send(params)
    xhr.onreadystatechange = ()=> {
      if (xhr.readyState == 4 && xhr.status == 200) {
        console.log("----请求成功----");
        let res = JSON.parse(xhr.responseText)
        callback(res)
      }
    }
  },
  serialize(obj:object) {
    let str = "";
    for (let key in obj) {
      str += key + "=" + obj[key] + "&";
    }
    return str.substring(0, str.length - 1);
  }
};